package com.kzj.controller;


import com.kzj.domain.Student;
import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.exception.MyException;
import com.kzj.global.re.Result;
import com.kzj.global.threadLocal.UserThreadLocal;
import com.kzj.global.utils.GlobalUtil;
import com.kzj.service.EletiveCourseService;
import com.kzj.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kzj
 * @since 2023-12-01
 */
@RestController
@RequestMapping("/student")
@Api(tags = "学生相关接口")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    EletiveCourseService eletiveCourseService;
    @ModelAttribute
    public void modelAttribute() {
        String type = UserThreadLocal.getType();
        if(!type.equals(GlobalConstant.STUDENT_TYPE))
            throw new MyException(GlobalConstant.USER_PERMISSION_DENIED);
    }
    @ApiOperation("抢课")
    @GetMapping("/robClasses/{courseId}")
    public Result robClasses(@PathVariable String courseId)
    {
        return studentService.robClasses(courseId);
    }
    @ApiOperation("获取课表")
    @GetMapping("/getSchedule")
    public Result getSchedule()
    {
        Student user = UserThreadLocal.getUser(Student.class);
        Integer term = GlobalUtil.getStudentTermByDateOfEnrollment(user.getDateOfEnrollment());
//        return eletiveCourseService.getTotalSchedule(user.getId(), term);
        return eletiveCourseService.getSelfSchedule(term, null);
    }
    @ApiOperation("获取下学期课表")
    @GetMapping("getNextSchedule")
    public Result getNextSchedule()
    {
        Student user = UserThreadLocal.getUser(Student.class);
        Integer term = GlobalUtil.getStudentTermByDateOfEnrollment(user.getDateOfEnrollment())+1;
//        return eletiveCourseService.getTotalSchedule(user.getId(), term);
        return eletiveCourseService.getSelfSchedule(term,null);
    }
    @ApiOperation("获取可选的课程")
    @GetMapping("optionalCourseList")
    public Result getOptionalCourseList()
    {
        return studentService.getOptionalCourseList();
    }
    @ApiOperation("修改学生信息")
    @PostMapping("/update")
    public Result updateStudent(@RequestBody Student student)
    {
        return studentService.updateStudent(student);
    }
    @ApiOperation("取消学生课程")
    @PostMapping("/cancelCourse/{courseId}")
    public Result cancelCourseList(@PathVariable String courseId)
    {
        return studentService.cancelCourse(courseId);
    }
    @ApiOperation("获取可取消的课程")
    @GetMapping("/getCancelCourse")
    public Result getCancelCourseList()
    {
        return studentService.getCancelCourseList();
    }
    @ApiOperation("获取本学期的考试信息")
    @GetMapping("/getCurrTermExamination/{studentId}")
    public Result getExamination(@PathVariable String studentId){return studentService.getCurrTermExamination(studentId);}
    @ApiOperation("获取成绩列表")
    @GetMapping("/ScoreList/{term}")
    public Result getScoreList(@PathVariable Integer term)
    {
        return studentService.getScoreList(term);
    }
}

